`
https://leetcode.cn/problems/delete-and-earn/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var deleteAndEarn = function (nums) {
  const maxVal = Math.max(...nums)
  const points = new Array(maxVal + 1).fill(0)
  for (const n of nums) points[n] += n

  // 把题目转换为 198 打家劫舍问题
  return rob(points)
};

// https://leetcode.cn/problems/house-robber/
var rob = function (nums) {
  const n = nums.length;
  // 记录 dp[i+1] 和 dp[i+2]
  let dp_i_1 = 0, dp_i_2 = 0;
  // 记录 dp[i]
  let dp_i = 0;
  for (let i = n - 1; i >= 0; i--) {
    dp_i = Math.max(dp_i_1, nums[i] + dp_i_2);
    dp_i_2 = dp_i_1;
    dp_i_1 = dp_i;
  }
  return dp_i;
};